﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

using Google.GData.Photos;
using Google.GData.Client;
using Google.GData.Extensions.MediaRss;

#pragma warning disable 3001, 3002, 3003
namespace McePicasa
{
    public class PicasaUser
    {
        /*
        public PicasaUser(string userId)
        {
            id = userId;
            albums = new List<PicasaAlbum>();
            LoadAlbums(userId);
        }
         * */

        public PicasaUser(string userId, AtomFeed feed)
        {
            id = userId;
            albums = new List<PicasaAlbum>();
            LoadAlbums(feed);
        }

        public void LoadAlbums(string userId)
        {
            PicasaFeed feed = PicasaAPI.GetUserAlbumFeed(userId);
            if (feed == null) return;

            foreach (PicasaEntry entry in feed.Entries)
            {
                PicasaAlbum album = new PicasaAlbum(entry, this);
                //PicasaAlbum album = new PicasaAlbum(entry.Title.Text, entry.FeedUri, (thumb.Attributes["url"] as string), int.Parse(entry.getPhotoExtensionValue(GPhotoNameTable.NumPhotos)));
                //AlbumAccessor albumAccessor = new AlbumAccessor(entry);
                albums.Add(album);
            }
            noOfAlbums = albums.Count;
        }

        public void LoadAlbums(AtomFeed feed)
        {
            if (feed == null) return;
            Cursor.Current = Cursors.WaitCursor;
            foreach (PicasaEntry entry in feed.Entries)
            {
                //MediaThumbnail thumb = entry.Media.Thumbnails[0];
                PicasaAlbum album = new PicasaAlbum(entry, this);
                //PicasaAlbum album = new PicasaAlbum(entry.Title.Text, entry.FeedUri, (thumb.Attributes["url"] as string), int.Parse(entry.getPhotoExtensionValue(GPhotoNameTable.NumPhotos)));
                //AlbumAccessor albumAccessor = new AlbumAccessor(entry);
                albums.Add(album);
            }
            noOfAlbums = albums.Count;
            Cursor.Current = Cursors.Default;
        }

        public static PicasaFeed CheckUserId(string userId)
        {
            try
            {
                PicasaFeed feed = PicasaAPI.GetUserAlbumFeed(userId);
                return feed;
            }
            catch (PicasaException e)
            {
                Log.LogIt(e.Message);
                return null;
            }
        }

        public PicasaAlbum GetAlbum(int index)
        {
            if (albums.Count <= index) return null;
            return albums[index];
        }

        public string Id
        {
            get { return id; }
            set { id = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public IList<PicasaAlbum> Albums
        {
            get { return albums; }
        }
        public int NoOfAlbums
        {
            get { return noOfAlbums; }
            set { noOfAlbums = value; }
        }

        public string Id2
        {
            get { return (id == "999wonders") ? "Sample" : id; }
        }

        private string id;
        private string name;
        private IList<PicasaAlbum> albums;
        private int noOfAlbums;


    }
}
